//
//  ExerTypeChooseVc.m
//  CareRingApp
//
//  Created by Linktop on 2023/10/18.
//

#import "ExerTypeChooseVc.h"
#import <Masonry/Masonry.h>
#import "ConfigModel.h"
#import "DBExercise.h"
#import "SelectItem.h"

extern const CGFloat EXEC_ITEM_HEIGHT;

@interface ExerTypeChooseVc ()

@property(strong, nonatomic)NSMutableArray<NSNumber *> *typeArray;

@property(strong, nonatomic)NSMutableArray<SelectItem *> *workOutItemArray;
@property(strong, nonatomic)UIScrollView *scrollView;
@property(strong, nonatomic)UIView *itemContent;
@end

@implementation ExerTypeChooseVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
   
    
    [self initUI];
    
}

-(void)initData {
    self.typeArray = [NSMutableArray new];
    
    [self.typeArray addObjectsFromArray:@[ @(EXER_WALK),  // 步行
                                           @(EXER_RUN),  // 室内跑步
                                           @(EXER_RUN_OUTDOOR),
                                           @(EXER_RIDE_INDOOR),
                                           @(EXER_RIDE_OUTDOOR),
                                           @(EXER_MOUTAIN_BIKE),
                                           @(EXER_SWIM), 
                                           @(EXER_Hatha_Yoga),//  哈他瑜伽
                                           @(EXER_Iyengar_Yoga), // 艾扬格瑜伽
                                           @(EXER_Vinyasa_Yoga),// 流瑜伽
                                           @(EXER_Power_Yoga),//  力量瑜伽
                                           @(EXER_Hot_Yoga),// 热瑜伽
                                           @(EXER_Ashtanga_Yoga),// 阿斯汤伽瑜伽
                                        ]];
    
    for (NSNumber *type in self.existTypeArray) { // 去除外部已存在的选项
        for (NSNumber *showType in self.typeArray) {
            if (type.intValue == showType.intValue) {
                [self.typeArray removeObject:showType];
                break;
            }
        }
        
    }
    
    self.workOutItemArray = [NSMutableArray new];
    
    for (NSNumber *type in self.typeArray) {
        
        SelectItem *runItem = [[SelectItem alloc]initWithStyle:NORMAL_STYLE];
        [runItem addBottomSepLine:SEPLINE_COLOR(0.3) Width:0.3];
        runItem.data = type; // 标记类型
        [runItem setLeftText:[DBExercise stringForExerType:type.intValue]];
        WEAK_SELF
        runItem.clickBLK = ^(SelectItem * _Nonnull view) {
            STRONG_SELF
            [strongSelf workoutCLick:view];
        };
        
        
        [self.workOutItemArray addObject:runItem];

    }
    
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width, CGRectGetMaxY(self.itemContent.frame));
}

-(void)workoutCLick:(SelectItem *)item {
    
    item.selected = YES;
    for (SelectItem *v in self.workOutItemArray) {
        if (v != item) {
            v.selected = !item.selected;
        }
    }
    
    if (self.callBack) {
        self.callBack(item.data);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)initUI {
    self.navigationItem.title = _L(L_NAV_TITL_SEL_EXEC); //选择锻炼
    
    self.itemContent = [UIView new];
    [self layoutTop:self.itemContent];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(VC_ITEM_MARGIN_HOR);
        make.trailing.equalTo(self.view.mas_trailing).inset(VC_ITEM_MARGIN_HOR);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        make.top.equalTo(self.view.mas_top);
    }];
    
    [self.scrollView addSubview:self.itemContent];
    [self.itemContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.itemContent.superview.mas_centerX);
        make.top.equalTo(self.itemContent.superview.mas_top).offset(20);
        make.leading.equalTo(self.itemContent.superview.mas_leading).offset(VC_ITEM_MARGIN_HOR);
        make.trailing.equalTo(self.itemContent.superview.mas_trailing).inset(VC_ITEM_MARGIN_HOR);

    }];
    
    
}

-(void)layoutTop:(UIView *)cnt {
    
    cnt.layer.cornerRadius = ITEM_CORNOR_RADIUS;
    cnt.layer.masksToBounds = YES;
    cnt.backgroundColor = ITEM_BG_COLOR;
    
    for (int i = 0; i < self.workOutItemArray.count; i++) {
        [cnt addSubview:self.workOutItemArray[i]];
    }
    for (int i = 0; i < self.workOutItemArray.count; i++) {
        SelectItem *v = self.workOutItemArray[i];
        [v mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(v.superview);
            if (i == 0) {
                make.top.equalTo(v.superview.mas_top);
            }
            if (i == self.workOutItemArray.count-1) {
                make.bottom.equalTo(v.superview.mas_bottom);
            }
            if (i > 0 && self.workOutItemArray.count > 1) {
                make.top.equalTo(self.workOutItemArray[i-1].mas_bottom);
            }
            make.height.equalTo(@(EXEC_ITEM_HEIGHT));

        }];
    }
   
    
}

-(UIScrollView *)scrollView
{
    if(!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}


@end
